package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.Point;
import cn.itsource.order.domain.PetAcquisitionOrder;
import cn.itsource.order.mapper.PetAcquisitionOrderMapper;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    /*通过DistanceUtil工具类找到最近店铺
			-先找到用户地址经纬度
			-传入店铺集合，找到最近店铺并绑定
            在user表中根据登录id找到userid并绑定
    通过店铺中管理员id，找到管理员电话
            使用SmsUtil工具类发送短信给店铺管理员*/
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id) {
        // 找到用户的经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 查找店铺集合并通过工具类查找离用户最近的店铺
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 绑定店铺
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 找出发布用户信息并绑定用户
        User user = userMapper.loadByLoginInfoId(id);
        searchMasterMsg.setUser_id(user.getId());
        // 保存寻主信息
        searchMasterMsgMapper.save(searchMasterMsg);
        // 查找店铺管理员并发送短信
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        // 假装发送短信
        System.out.println("新的宠物寻主订单：【"+searchMasterMsg.getTitle()+"】");

    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        // 添加查询条件状态为0 待完成
        query.setState(0);
        // 根据传来的id 查询待完成的寻主信息
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        // 判断是否是超级管理员（没有对应的shop_id）
        if (employee.getShop_id() != null) {
            // 如果有对应的店铺，则附上店铺id
             query.setShopId(employee.getShop_id());
        }
        PageList<SearchMasterMsg> list = super.queryForPage(query);


        return list;
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        // 添加查询条件状态为0 待完成
        query.setState(1);
        // 根据传来的id 查询待完成的寻主信息
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        // 判断是否是超级管理员（没有对应的shop_id）
        if (employee.getShop_id() != null) {
            // 如果有对应的店铺，则附上店铺id
            query.setShopId(employee.getShop_id());
        }
        PageList<SearchMasterMsg> list = super.queryForPage(query);


        return list;
    }

    // 处理宠物 新增详细信息
    @Override
    public void handle(Pet pet, Long loginInfoId) {
        /*
        将宠物的状态从待处理改为已处理
        保存宠物信息 返回主键，再保存宠物的详细信息
         */
        System.out.println(pet);
        searchMasterMsgMapper.upHandle(pet.getSearch_master_msg_id());
        petService.add(pet);
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
        //5.@TODO 生成支付单并支付
        System.out.println("支付成功！"+pet.getCostprice());
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
